﻿using InParkRedemptionAPI.Controllers;
using InParkRedemptionAPI.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using RapidSalesAdminPortalAPI.DataAccess;
using RapidSalesAdminPortalAPI.Models;
using WebAPI.Common;
using WebAPI.Controllers;
using WebAPI.Util;

namespace RapidSalesAdminPortalAPI.Controllers
{
    //[Authorize]
    [Route("api/[controller]")]
    [ApiController]
    public class FrIpTransactionHistoryController : ParentController
    {
        private readonly RS_AdmPortalContext _context;
        private readonly IConfiguration _configuration;
        private readonly ILogUtility _logUtility;
        private readonly ILogger _logger;
        private readonly IDataProtectorUtility _dataProtectorUntility;
        public FrIpTransactionHistoryController(RS_AdmPortalContext context, IConfiguration configuration, ILogUtility logUtility, ILogger<FrIpTransactionHistoryController> logger, IDataProtectorUtility dataProtectorUntility)
        {
            _context = context;
            _configuration = configuration;
            _logUtility = logUtility; // sample code for DI of common utility class
            _logger = logger;
            _dataProtectorUntility = dataProtectorUntility;
        }

        [HttpPost("TransactionHistoryList")]
        public async Task<ActionResult<ResultMsg>> TransactionHistoryList([FromBody] TransactionHistoryListRequest requestModel)
        {
            var resultMsg = new ResultMsg();
            try
            {
                IQueryable<FrIpTransactionHistory> paComont = _context.FrIpTransactionHistories.AsQueryable();

                if (!string.IsNullOrWhiteSpace(requestModel.LastUpdatedOnBegin))
                    paComont = paComont.Where(x => x.LastUpdatedOn > Convert.ToDateTime(requestModel.LastUpdatedOnBegin));
                if (!string.IsNullOrWhiteSpace(requestModel.LastUpdatedOnEnd))
                    paComont = paComont.Where(x => x.LastUpdatedOn < Convert.ToDateTime(requestModel.LastUpdatedOnEnd));
                if (!string.IsNullOrWhiteSpace(requestModel.TransactionStatus))
                    paComont = paComont.Where(x => x.TransactionStatus== requestModel.TransactionStatus);
                if (!string.IsNullOrWhiteSpace(requestModel.OrderNo))
                    paComont = paComont.Where(x => x.OrderNo.Contains(requestModel.OrderNo));
                if (!string.IsNullOrWhiteSpace(requestModel.DataFeedResults))
                    paComont = paComont.Where(x => x.DataFeedResults.Contains(requestModel.DataFeedResults));
                if (!string.IsNullOrWhiteSpace(requestModel.DataFeedPaymentRef))
                    paComont = paComont.Where(x => x.DataFeedPaymentRef.Contains(requestModel.DataFeedPaymentRef));
                if (!string.IsNullOrWhiteSpace(requestModel.DataFeedBankRef))
                    paComont = paComont.Where(x => x.DataFeedBankRef.Contains(requestModel.DataFeedBankRef));
                if (requestModel.DataFeedAmount > 0)
                    paComont = paComont.Where(x => x.DataFeedAmount== requestModel.DataFeedAmount.ToString()); 
                if (!string.IsNullOrWhiteSpace(requestModel.DataFeedPaymentMethod))
                    paComont = paComont.Where(x => x.DataFeedPaymentMethod.Contains(requestModel.DataFeedPaymentMethod));
                if (!string.IsNullOrWhiteSpace(requestModel.DataFeedAlertCode))
                    paComont = paComont.Where(x => x.DataFeedAlertCode.Contains(requestModel.DataFeedAlertCode));

                var paData = paComont;

                if (!string.IsNullOrWhiteSpace(requestModel.Field) && paComont.Count() > 0)
                    paData = DBHelper.DataSorting(paData, requestModel.Field, requestModel.Order);
                else
                    paData = DBHelper.DataSorting(paData, "CreateDate", "desc");

                var AdministratorAccount = await BasePageOutputExtensions.ToPagedListAsync(paData.ToList(), requestModel.Page, requestModel.PageSize);

                return resultMsg.SetResultMsg(
                     (int)ResultCode.Success,
                     HelperUtility.GetDescription(ResultCode.Success),
                     AdministratorAccount);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return resultMsg.SetResultMsg(
                     (int)ResultCode.ErrorOnRequestUnMarshalling,
                     HelperUtility.GetDescription(ResultCode.ErrorOnRequestUnMarshalling),
                     null);
            }
        }

        [HttpPost("{id}")]
        public async Task<ActionResult<ResultMsg>> TransactionHistoryById(Guid id)
        {
            var resultMsg = new ResultMsg();
            try
            {
                var model = await _context.FrIpTransactionHistories.FirstOrDefaultAsync(x=>x.Id == id);

                return resultMsg.SetResultMsg(
                     (int)ResultCode.Success,
                     HelperUtility.GetDescription(ResultCode.Success),
                     model);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return resultMsg.SetResultMsg(
                     (int)ResultCode.ErrorOnRequestUnMarshalling,
                     HelperUtility.GetDescription(ResultCode.ErrorOnRequestUnMarshalling),
                     null);
            }
        }
    }
}
